<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;



use app\common\model\ForumBbsComment;
use app\common\model\ForumBbsCommentFabulous;
use app\common\model\ForumBbsFabulous;
use app\common\model\ForumFollow;
use app\common\model\Member;
use think\Db;

/**
 * 用户接口逻辑
 */
class Bbs extends ApiBase
{


    /**
     * 置顶头条
     */

    public function  bbs_article($data){
        $level=!empty($data['level'])?$data['level']:1;
        $where['a.level']=$level;
        $where['c.type']=1;
        $this->modelArticle->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
        ];
        $where['a.status']=1;
        $this->modelArticle->join = $join;
        $list=$this->modelArticle->getList($where,'a.cover_id,a.name,a.category_id,a.describe,a.id,c.name as cate_name','a.sort desc',false);

        foreach ($list as $k=>$v){
                $list[$k]['image']=$v->image;
        }


        return $list;
    }


    /**
     * 搜索头条,知识库
     */

    public function  bbs_article_search($data){
        $type=!empty($data['type'])?$data['type']:1;
        $where['c.type']=$type;
        if(!empty($data['search'])){
            $where['a.name|a.content|a.describe']=['like','%'.$data['search'].'%'];

        }
        if(!empty($data['level'])){
            $where['a.level']=$data['level'];

        }
        if(!empty($data['category_id'])){
            $where['a.category_id']=$data['category_id'];
        }
        $this->modelArticle->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'article_category c', 'a.category_id = c.id'],
        ];
        $where['a.status']=1;
        $this->modelArticle->join = $join;
        $list=$this->modelArticle->getList($where,'a.cover_id,a.name,c.name as cate_name,a.describe,a.id,a.member_id,a.create_time,a.category_id','a.is_hot desc ,a.sort desc,a.id desc');



        foreach ($list as $k=>$v){
            $list[$k]['image']=$v->image;
            $list[$k]['addtime']=date('Y-m-d',$v->create_time);
            $list[$k]['user_name']=$v->user->username;
            unset($v->user);
        }

        return $list;
    }



    /***
     * 文章详情
     */
    public function  bbs_knowledge_base(){

       $cate=$this->modelArticleCategory->getList(['type'=>2],'id,name','',false);


       foreach ($cate as $k=>$v){
           $cate[$k]['atricle']=$this->modelArticle->getList(['category_id'=>$v['id'],'level'=>0],'id,name','sort desc ,create_time desc',false);
       }
       return $cate;
    }


    /***
     * 论坛中心
     */
    public function  bbs_center(){

       $member_count=Member::where(['status'=>1])->count();
       $bbs_count=$this->modelForumBbs->where(['status'=>1])->count();
        $bbs_comment=$this->modelForumBbsComment->where(['status'=>1])->count();
        $first_group=getConfig('qq_group_first');
        $second_group=getConfig('qq_group_second');
        $price_bbs_group=getConfig('price_bbs_group');
        $dig_bbs_group=getConfig('dig_bbs_group');

        return ['member_count'=>$member_count,'bbs_count'=>$bbs_count,'bbs_comment'=>$bbs_comment,'first_group'=>$first_group,'second_group'=>$second_group,'price_bbs_group'=>$price_bbs_group,'dig_bbs_group'=>$dig_bbs_group];
    }



    /***
     * 论坛中心列表
     */
    public function  bbs_center_list($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $where=['state'=>1];
        if(!empty($data['cate_id'])){
            $where['cate_id']=$data['cate_id'];
        }
        if(!empty($data['tags'])){
            $where['tags']=['like','%,'.$data['tags'].',%'];
        }
        if(!empty($data['search'])){
            $where['name| content']=['like','%'.$data['search'].'%'];
        }

        $list=$this->modelForumBbs->getList($where,'id,user_id,name,content,cate_id,state','sort desc');

        foreach ($list as $k=>$v){
            $list[$k]['username']=$v->user->username;
            $list[$k]['cate_name']=$v->cate['name'];
            $list[$k]['avtar']=$v->user->avtar;
            $where1=['user_id'=>$uid,'uid'=>$uid,'is_hide'=>0];
            $list[$k]['reply_count1']= ForumBbsComment::where(['bbs_id'=>$v['id']])->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();
            if($reply=$v->endreply($uid)){
                $list[$k]['reply_username']=$v->user()->where(['id'=>$reply['user_id']])->value('username');
                $list[$k]['time']=exchange_time($reply['create_time']);

            }
            unset($v->cate);
            unset($v->reply);
            unset($v->user);

        }

        return $list;

    }


    /**
     * 论坛内容
     */
    public function  bbs_info($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelForumBbs->getInfo(['id'=>$data['id']]);
        if(!$info){
            return ajaxreturn(1,lang('该帖已被删除'));
        }

        $info->read_count+=1;
        $info->save();
       $coment=[];
        foreach ($info->reply as  $k=> $v){
            if($v['is_hide']==0){
                $coment[$k]['id']=$v['id'];
                $coment[$k]['uid']=$v['uid'];
                $coment[$k]['user_id']=$v['user_id'];
                $coment[$k]['user_name']=$v->user->username;
                $coment[$k]['content']=$v['content'];
                $coment[$k]['avtar']=$v->user->avtar;
                $coment[$k]['zan']=$v->zan($uid);
                $coment[$k]['zan_count']=$v->zan_count;
                $coment[$k]['follow']=$v->follow($uid);
                $coment[$k]['time']=exchange_time($v['create_time']);
                $coment[$k]['username'] = $v->user->username;
                unset($v->user);

            }elseif($v['uid']==$uid || $v['user_id']==$uid){
                $coment[$k]['id']=$v['id'];
                $coment[$k]['uid']=$v['uid'];
                $coment[$k]['user_id']=$v['user_id'];
                $coment[$k]['user_name']=$v->user->username;
                $coment[$k]['content']=$v['content'];
                $coment[$k]['avtar']=$v->user->avtar;
                $coment[$k]['zan']=$v->zan($uid);
                $coment[$k]['zan_count']=$v->zan_count;
                $coment[$k]['follow']=$v->follow($uid);
                $coment[$k]['time']=exchange_time($v['create_time']);
                $coment[$k]['username'] = $v->user->username;
                unset($v->user);
            }

         }
        unset($info->reply);

        $info['comment']=array_merge($coment);
        $info['follow']=$info->follow($uid);
        $info['zan']=$info->zan($uid);
        $info['avtar']=$info->user->avtar;
        $info['images']=$info->images;
        $info['user_name']=$info->user->avtar;
        unset($info->user);
        return $info;


    }



    /***
     * 回帖
     */
    public function  reply_bbs($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=$this->modelForumBbs->getInfo(['id'=>$data['bbs_id']]);
        if(!$info){
            return ajaxreturn(1,lang('该帖已被删除'));
        }
        if(empty($data['content'])){
            return ajaxreturn(1,lang('内容不能为空'));
        }
        if(!empty($data['comment_id'])){
           $comment=$this->modelForumBbsComment->getInfo(['id'=>$data['bbs_id']]);
           $data['uid']=$comment['user_id'];
        }
        $data['user_id']=$uid;
        Db::startTrans();
        try{
            $this->modelForumBbsComment->setInfo($data);
            //通知用户
            if(empty($data['comment_id'])){
                $data['uid']=$info['user_id'];
            }
            $this->modelForumNotice->setInfo([
                'user_id'=>$data['uid'],
                'uid'=>$uid,
                'bbs_id'=>$data['bbs_id']
            ]);
            Db::commit();
            sleep(2);
            return ajaxreturn(0,lang('回复成功'));
        }catch (\Exception $e){
            Db::rollback();
            return ajaxreturn(1,$e->getMessage());
        }
    }

    /***
     * 点赞
     */
    public function  bbs_zan($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        if(ForumBbsFabulous::get(['status'=>1,'user_id'=>$uid,'bbs_id'=>$data['id']])){
            return ajaxreturn(1,lang('您已赞过'));
        }
        ForumBbsFabulous::create([
            'user_id'=>$uid,
            'bbs_id'=>$data['id']
        ]);
        return ajaxreturn(0,lang('成功'));
    }


    /***
     * 点赞
     */
    public function  bbs_comment_zan($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        if(ForumBbsCommentFabulous::get(['status'=>1,'user_id'=>$uid,'bbs_id'=>$data['id']])){
            return ajaxreturn(1,lang('您已赞过'));
        }
        ForumBbsCommentFabulous::create([
            'user_id'=>$uid,
            'bbs_id'=>$data['id']
        ]);
        return ajaxreturn(0,lang('成功'));
    }


    /***
     * 关注/取消关注
     */
    public function  bbs_follow($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $info=ForumFollow::get(['user_id'=>$uid,'uid'=>$data['uid']]);

        if($data['type']==1){
            if($uid==$data['uid']){
                return ajaxreturn(1,lang('自己不能关注自己'));
            }
            if($info && $info['status']==1){
                return ajaxreturn(1,lang('您已关注对方'));
            }
            if($info && $info['status']== -1){
                ForumFollow::update(['status'=>1],[
                    'user_id'=>$uid,
                    'uid'=>$data['uid']
                ]);
            } else{
                ForumFollow::create([
                    'user_id'=>$uid,
                    'uid'=>$data['uid']
                ]);
            }
        }else{
            $uid=decoded_user_token($data['user_token'],'member_id');
            if($info && $info['status']==-1){
                return ajaxreturn(1,lang('您已取消关注对方'));
            }
            ForumFollow::update(['status'=>-1],[
                'user_id'=>$uid,
                'uid'=>$data['uid']
            ]);
        }


        return ajaxreturn(0,lang('成功'));
    }



    /***
     * 用户通知列表
     */
    public function  bbs_notice($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $where['user_id']=$uid;

        $list=$this->modelForumNotice->getList($where,'*','id desc');

        $where['is_read']=0;
        $where['status']=1;
        $this->modelForumNotice->where($where)->update(['is_read'=>1]);
        foreach ($list as $k=>$v){
            $list[$k]['username']=$v->user->username;
            $list[$k]['add_time']=date('Y-m-d H:i:s',$v->create_time);
            $list[$k]['avtar']=$v->user->avtar;
            $list[$k]['bbs_title']=$v->bbs['name'];
            $list[$k]['to_user_name']=$v->touser->username;
            unset($v->user);
            unset($v->bbs);
            unset($v->touser);
        }

        return $list;

    }


    /***
     * 清除用户通知列表
     */
    public function  clear_bbs_notice($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $where['user_id']=$uid;
        $where['status']=1;
        $this->modelForumNotice->where($where)->update(['status'=>-1]);
        return ajaxreturn(0,lang('成功'));

    }

    /**
     * 用户主页
     */
    public function user_homepage($data){
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }

        $avtar=$user->avtar;
        $follow_count=$this->modelForumFollow->where(['uid'=>$data['uid'],'status'=>1])->count();
        $follow=$this->modelForumFollow->where(['user_id'=>$data['uid'],'status'=>1])->count();
        return ['avtar'=>$avtar,'follow_count'=>$follow_count,'follow'=>$follow,'uid'=>$data['uid']];

    }


    /***
     * 我的帖子
     */
    public function  my_bbs($data){
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }
        $where['user_id']=$data['uid'];
        $list=$this->modelForumBbs->getList($where,'id,user_id,name,content,cate_id,create_time,state','create_time desc');
        foreach ($list as $k=>$v){

                $list[$k]['cate_name']=$v->cate['name'];
                $list[$k]['reply_count']= $v->reply_count;
                 $list[$k]['time']=exchange_time($v['create_time']);

                unset($v->reply);
                unset($v->cate);
        }

        return $list;
    }


    /***
     * 我的回帖
     */
    public function  my_bbs_comment($data){
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }
        $where['user_id']=$data['uid'];
        $list=$this->modelForumBbsComment->getList($where,'id,user_id,bbs_id,content,create_time,is_hide','create_time desc');
        $time=time();
        foreach ($list as $k=>$v){

                $list[$k]['name']=$v->bbs['name'];
                $minute=floor(($time-$v['create_time'])/60);
                $hour=floor($minute/60);
                $day=floor($hour/24);
                $month=floor($day/30);
                if(floor(($time-$v['create_time']))<1){
                    $list[$k]['time']=floor(($time-$v['create_time'])).lang('秒前');
                }elseif($minute<60){
                    $list[$k]['time']=$minute.lang('分钟前');

                }elseif($hour<24){
                    $list[$k]['time']=$hour.lang('小时前');

                }elseif($day<30){
                    $list[$k]['time']=$day.lang('天前');
                }else{

                    $list[$k]['time']=$month.lang('月前');
                }

                unset($v->bbs);
        }

        return $list;
    }


    /***
     * 我的关注者
     */
    public function  my_follow($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }
        $where['uid']=$data['uid'];
        $list=$this->modelForumFollow->getList($where,'id,user_id,create_time','create_time desc');

        foreach ($list as $k=>$v){
            $list[$k]['follow']=$v->follow($uid);
            $list[$k]['username']=$v->user['username'];
            $list[$k]['avtar']=$v->user->avtar;
            unset($v->user);

        }

        return $list;
    }



    /***
     * 正在关注
     */
    public function  follow_user ($data){
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }
        $where['user_id']=$data['uid'];
        $list=$this->modelForumFollow->getList($where,'id,uid,create_time','create_time desc');

        foreach ($list as $k=>$v){

            $list[$k]['username']=$v->touser['username'];
            $list[$k]['avtar']=$v->touser->avtar;
            unset($v->touser);

        }

        return $list;
    }


    /***
     * 他的文章
     */
    public function  article_user ($data){
        $user=$this->modelMember->getInfo(['id'=>$data['uid']]);
        if(!$user){
            return ajaxreturn(1,lang('用户不存在'));
        }
        $where['member_id']=$data['uid'];
        $list=$this->modelArticle->getList($where,'id,cover_id,create_time,name,describe,category_id','create_time desc');

        foreach ($list as $k=>$v){

            $list[$k]['cate_name']=$v->cate['name'];
            $list[$k]['image']=$v->image;
            $list[$k]['addtime']=date('Y-m-d',$v->create_time);
            unset($v->cate);

        }

        return $list;
    }











































    /**
     * 发布话题
     */
    public function issue_topic($data = [])
    {
        $uid=decoded_user_token($data['user_token'],'member_id');

        if(empty($data['cate_id'])){
            return ajaxreturn(1,lang('请选择话题分类'));
        }
        if(empty($data['title'])){
            return ajaxreturn(1,lang('请输入标题'));
        }
        $data['name']=$data['title'];
        if(empty($data['content'])){
            return ajaxreturn(1,lang('请输入内容'));
        }

        $data['user_id']=$uid;
        $this->modelForumBbs->setInfo($data);
        return ajaxreturn(0,lang('发布成功'));


    }
















}
